﻿using System;
using System.IO;
using System.Web;
using System.Xml;
using System.Configuration;
using SyCODE.AjaxUpload;
using SyCODE.AjaxUpload.Providers;
using SyCODE.AjaxUpload.Configuration;
using SyCODE.Component;
using System.Collections;

namespace SyCODE.Blog.Uploader
{
    /// <summary>
    /// UploadProgress.aspx 的处理程序
    /// 实现 AjaxUpload 上传过程的状态和进度查询
    /// </summary>
    public class UploadProgressHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string uploadId = context.Request.Form["uploadId"];

            context.Response.ContentType = "text/xml";

            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);

            writer.WriteStartDocument();
            writer.WriteStartElement("root");

            UploadStatus status = HttpUploadModule.GetUploadStatus(uploadId);

            if (status != null)
            {
                if (status.State == AjaxUpload.UploadState.Error)
                {
                    writer.WriteAttributeString("st", "ERROR");
                    writer.WriteAttributeString("er", status.Description);
                    HttpUploadModule.RemoveFrom(uploadId);
                }
                else if (status.State == AjaxUpload.UploadState.ErrorMaxRequestLengthExceeded)
                {
                    writer.WriteAttributeString("st", "ERROR");
                    writer.WriteAttributeString("er", StringResource.GetField("filelengthtoolong"));
                    HttpUploadModule.RemoveFrom(uploadId);
                }
                else
                {
                    writer.WriteAttributeString("st", status.State.ToString());
                    writer.WriteAttributeString("fs", status.ContentLength.ToString());
                    writer.WriteAttributeString("up", status.Position.ToString());
                }
                if (status.State == AjaxUpload.UploadState.Complete)
                {
                    UploadLocationProviderConfiguration configuration1 = (UploadLocationProviderConfiguration)ConfigurationManager.GetSection("ajaxUpload/uploadSettings");
                    string location = configuration1.CustomSettings["location"];
                    ArrayList savedFileNames = status.GetSavedFileNames();

                    string dateformat = StringResource.GetField("dateformat");
                    for (int i = 0; i < savedFileNames.Count; i++)
                    {
                        string filename = location + savedFileNames[i].ToString();
                        writer.WriteStartElement("file");
                        writer.WriteAttributeString("name", filename.Replace('\\', '/'));

                        FileInfo fileInfo = new FileInfo(AppPath.GetAppRootPath() + filename.TrimStart(new char[] { '/', '\\' }));
                        if (fileInfo.Exists)
                        {
                            writer.WriteAttributeString("size", fileInfo.Length.ToString());
                            writer.WriteAttributeString("date", fileInfo.LastAccessTime.ToString(dateformat));
                        }

                        writer.WriteEndElement();
                    }

                    HttpUploadModule.RemoveFrom(uploadId);
                }
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}